﻿using System;
using System.Collections.Generic;
using System.Text;
using WebServer = SysCommon.Business.CVSS.WS.DCS.WS_DCS_BizCtrlImpl_New;
using Query = SysCommon.Entity.CVSS.WS.DCS.BaseQueryCondition_Ety;
using System.Windows.Forms;
using BindingData = SysCommon.Entity.Business.DCS.View.View_Escort_Ety;
using SaveData = SysCommon.Entity.CVSS.WS.DCS.Escort_Ety;
using CVSS.Client.BDM.Business;

namespace CVSS.Client.BDM.UI
{
    partial class EscortManageFormMDT
    {
        private BindingData[] _bindingData = null;
        private SysCommon.Enums.CVSS.WS.DCS.DBOperatonEnum _operation;

        object dvp_DataView_OnDataChangedHandlerEvent(object sender, SysCommon.UI.DataGridViewForPageCtrl.OnDataChangedHandlerEventArgs e)
        {
            return ShowEntityHelper.SelectionChanged<
                BindingData>(_bindingData, e);
        }

        private void Save()
        {
            try
            {
                SaveData data = new SaveData();
                DataGridViewRow row = form.dvp_DataView.dg_DataView.CurrentRow;
                if (row != null)
                {
                    //BindingData bind = form.dvp_DataView.dg_DataView.CurrentRow.DataBoundItem as
                    //    BindingData;
                    //bind.SEXNAME
                    //if (bind != null)
                    //{
                    data.ID = (row.Tag as SysCommon.Entity.Business.DCS.View.View_Escort_Ety).ID;
                    //}
                }

                data.BIRTHDAY = form.dtp_Birthday.Value;
                data.DANGERCOMPANYID = Convert.ToInt32(form.cmb_CompanyName.SelectedValue);
                data.ESCORTCARD = form.txt_EscortID.Text.Trim();
                data.IDCARD = form.txt_IDCARD.Text.Trim();
                data.NAME = form.txt_Name.Text.Trim();
                data.SEX = Convert.ToInt16(form.cmb_Sex.SelectedValue);
                data.TEL = form.txt_TEL.Text.Trim();
                
                WebServer ws = new WebServer();
                bool re = ws.DangerEscortOperation(data, _operation);
                if (!re)
                {
                    MessageBox.Show("保存失败。");
                    return;
                }
                form.btn_Modify.Text = "修改";
                form.btn_Add.Text = "添加";
                form.btn_Save.Enabled = false;
                BeginQuery();
            }
            catch (Exception ex)
            {
                SysCommon.Global.mainLog.Error("调用Save()出错", ex);
            }
            finally
            {
                form.btn_Add.Enabled = true;

                form.txt_Name.Enabled = false;
                form.txt_IDCARD.Enabled = false;
                form.txt_EscortID.Enabled = false;
                form.txt_TEL.Enabled = false;
                form.cmb_CompanyName.Enabled = false;
                form.cmb_Sex.Enabled = false;
                form.dtp_Birthday.Enabled = false;

            }
        }

        #region 初始化DataGridView
        private void InitDataGridView()
        {
            try
            {
                string[] en_Cols = new string[] { 
                    "NAME", "IDCARD", "ESCORTCARD", 
                    "DANGERCOMPANYNAME", "SEXNAME","BIRTHDAY" ,"TEL"};
                string[] cn_Cols = new string[] { 
                    "押运员姓名", "身份证号码", "押运证号码", 
                    "所属企业", "性别","出生年月","联系电话" };
                SysCommon.Tools.ShowEntityForDataGridView.SetDefaultDataGrigStyle2(
                    form.dvp_DataView.dg_DataView);
                form.dvp_DataView.Init(0, 0, 0, null,cn_Cols, en_Cols );
                SysCommon.Tools.ShowEntityForDataGridView.EntityToView(
                    cn_Cols, en_Cols, new BindingData[0], form.dvp_DataView.dg_DataView);
                form.dvp_DataView.dg_DataView.AutoSizeColumnsMode =
                     DataGridViewAutoSizeColumnsMode.Fill;
                form.btn_Save.Enabled = false;
                form.btn_Modify.Enabled = false;
                form.btn_Delete.Enabled = false;
                SetControlEnable(false);
            }
            catch (Exception ex)
            {
                SysCommon.Global.mainLog.Error("调用DangerEnterpriseManageFormMDT.InitDataGridView()出错", ex);
            }
        }
        #endregion

        #region 响应查询事件
        private void BeginQuery()
        {
            try
            {
                WebServer ws = new WebServer();
                Query query = new Query();
                query.KeyWord = form.txt_KeyWord.Text.Trim();
                byte[] bs = ws.QueryDangerEscortInfoWithCompress(query);
                if (bs != null)
                {
                    BindingData[] ds = SysCommon.Tools.CompressionHelper.DeCompressEntity(bs) as
                        BindingData[];
                    _bindingData = ds;
                    if (_bindingData == null)
                    {
                        _bindingData = new BindingData[0];
                    }
                    ShowEntityHelper.ShowFlowDataInfo<BindingData>(
                        _bindingData, form.dvp_DataView);
                }
                
                
            }
            catch (Exception ex)
            {
                SysCommon.Global.mainLog.Error("调用DangerEnterpriseManageFormMDT.BeginQuery()出错", ex);
            }
        }
        #endregion

        #region 响应添加事件
        private void Add()
        {
            try
            {
                //判断是否取消添加
                string btnText = form.btn_Add.Text;
                if (btnText.Contains("取消"))  //单击了取消添加按钮
                {                    
                    //把控件设为不可见
                    SetControlEnable(false);
                    form.btn_Save.Enabled = false;
                    form.btn_Delete.Enabled = true;
                    form.btn_Modify.Enabled = true;
                    form.btn_Add.Enabled = true;
                    //把按钮的Text设为添加
                    form.btn_Add.Text = "添加";
                    //绑定当前选中的项
                    dg_DataView_SelectionChanged(null, null);
                }
                else  //单击了添加按钮
                {
                    UnBindingData();

                    _operation = SysCommon.Enums.CVSS.WS.DCS.DBOperatonEnum.AddRecord;
                    //把控件设为可见
                    SetControlEnable(true);
                    //设置控件的显示文本为空
                    //押运员姓名
                    form.txt_Name.Text = "";
                    //身份证号码
                    form.txt_IDCARD.Text = "";
                    //押运证号码
                    form.txt_EscortID.Text = "";
                    //所属企业
                    form.cmb_CompanyName.Text = "";
                    //性别
                    form.cmb_Sex.Text = "";
                    //出生年月
                    form.dtp_Birthday.Value = System.DateTime.Now;
                    //联系电话
                    form.txt_TEL.Text = "";
                    //设置按钮的可用性
                    form.btn_Delete.Enabled = false;
                    form.btn_Save.Enabled = true;
                    form.btn_Modify.Enabled = false;
                    form.btn_Add.Text = "取消添加";
                }
            }
            catch (Exception x)
            {
                SysCommon.Global.mainLog.Error("调用DangerCargoManageFormMDT.Add()出错", x);
            }
        }
        #endregion

        #region 响应删除事件
        private void Delete()
        {
            try
            {
                if (MessageBox.Show("确定要删除吗？", "删除", MessageBoxButtons.YesNo,
                    MessageBoxIcon.Question) == DialogResult.No)
                {
                    return;
                }
                //获取当前的选择项
                DataGridViewRow row = form.dvp_DataView.dg_DataView.CurrentRow;
                if (row != null)
                {
                //    BindingData data = row.DataBoundItem as BindingData;
                    SaveData saveData = new SaveData();

                    saveData.ID = (row.Tag as SysCommon.Entity.Business.DCS.View.View_Escort_Ety).ID;

                    //if (data != null)
                    //{
                        //调用WebService删除信息
                        //SaveData saveData = new SaveData();
                        //saveData.ID = data.ID;
                        WebServer ws = new WebServer();
                        bool re = ws.DangerEscortOperation(saveData,
                             SysCommon.Enums.CVSS.WS.DCS.DBOperatonEnum.DeleteRecord);
                        if (!re)
                        {
                            MessageBox.Show("保存失败。");
                            return;
                        }
                        BeginQuery();
                    //}
                }
            }
            catch (Exception ex)
            {
                SysCommon.Global.mainLog.Error("调用DangerCargoManageFormMDT.Delete()出错", ex);
            }
        }
        #endregion

        #region 响应修改事件
        private void Modify()
        {
            try
            {
                //判断是否取消修改
                string btnText = form.btn_Modify.Text;
                if (btnText.Contains("取消"))  //单击了取消修改按钮
                {
                    //把控件设为不可编辑
                    SetControlEnable(false);
                    form.btn_Add.Enabled = true;
                    //把按钮的Text设为修改
                    form.btn_Modify.Text = "修改";
                    //绑定当前选中的项

                    

                    dg_DataView_SelectionChanged(null, null);
                }
                else
                {
                    UnBindingData();

                    _operation = SysCommon.Enums.CVSS.WS.DCS.DBOperatonEnum.ModifyRecord;
                    //把控件设为编辑状态
                    SetControlEnable(true);
                    //把保存按钮设为可用
                    form.btn_Save.Enabled = true;
                    //把添加按钮变为不可用
                    form.btn_Add.Enabled = false;
                    //把删除按钮变为不可用
                    form.btn_Delete.Enabled = false;
                    form.btn_Modify.Text = "取消修改";
                }
            }
            catch (Exception ex)
            {
                SysCommon.Global.mainLog.Error("调用DangerCargoManageFormMDT.Modify()出错", ex);
            }
        }
        #endregion

        #region 数据绑定
        /// <summary>
        /// 数据绑定
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void dg_DataView_SelectionChanged(object sender, EventArgs e)
        {
            try
            {
                DataGridViewRow row = form.dvp_DataView.dg_DataView.CurrentRow;
                if (row != null)
                {
                    //检查是否处于编辑状态
                    if (form.btn_Save.Enabled == true)
                    {
                        DialogResult dr = MessageBox.Show("编辑的方案未保存，要放弃保存吗？", "危险品运输计划管理", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                        if (dr == DialogResult.No)
                        {
                            return;
                        }

                        

                        form.btn_Save.Enabled = false;
                        form.btn_Add.Enabled = true;
                        form.btn_Delete.Enabled = true;
                        form.btn_Modify.Enabled = true;
                        form.btn_Add.Text = "添加";
                        form.txt_EscortID.Enabled = false;
                        form.txt_IDCARD.Enabled = false;
                        form.txt_Name.Enabled = false;
                        form.txt_TEL.Enabled = false;
                        form.cmb_Sex.Enabled = false;
                        form.cmb_CompanyName.Enabled = false;
                    
                        ////押运员姓名
                        //form.txt_Name.Text = data.NAME;
                        ////身份证号码
                        //form.txt_IDCARD.Text = data.IDCARD;
                        ////押运证号码
                        //form.txt_EscortID.Text = data.ESCORTCARD;
                        ////所属企业
                        //form.cmb_CompanyName.SelectedValue = data.DANGERCOMPANYID;
                        ////性别
                        //form.cmb_Sex.SelectedValue = data.SEX;
                        ////出生年月
                        //form.dtp_Birthday.Value = data.BIRTHDAY;
                        ////联系电话
                        //form.txt_TEL.Text = data.TEL;
                        ////设置按钮的可用性
                        //form.btn_Delete.Enabled = true;
                        //form.btn_Modify.Enabled = true;
                        //form.btn_Save.Enabled = false;
                        //设置控件为不可编辑
                        //SetControlEnable(false);
                        //return;
                    }
                }
                //SysCommon.Entity.Business.DCS.View.View_Escort_Ety[] m_Etys = (SysCommon.Entity.Business.DCS.View.View_Escort_Ety[])form.dvp_DataView.dg_DataView.DataSource;
                //BindingData(m_Etys);
                int index=form.dvp_DataView.dg_DataView.SelectedRows[0].Index;
                SysCommon.Entity.Business.DCS.View.View_Escort_Ety temp;
                temp = form.dvp_DataView.dg_DataView.Rows[index].Tag as SysCommon.Entity.Business.DCS.View.View_Escort_Ety;
                BindingData(temp);
                //设置按钮的可用性
                
                    //form.btn_Delete.Enabled = false;
                    //form.btn_Modify.Enabled = false;
                    //form.btn_Save.Enabled = false;
                    //form.btn_Add.Enabled = true;
                
            }
            catch (Exception ex)
            {
                SysCommon.Global.mainLog.Error(
                    "调用DangerEnterpriseManageFormMDT.dg_DataView_SelectionChanged()出错", ex);
            }
        }
        #endregion

        #region 设置控件的可编辑状态
        private void SetControlEnable(bool enable)
        {
            EditorHelper.SetControlEnable(enable,
                new Control[] { form.txt_Name,form.txt_IDCARD,
                        form.txt_EscortID,form.txt_EscortID,form.cmb_CompanyName,
                        form.cmb_Sex,form.dtp_Birthday,form.txt_TEL});
        }
        #endregion

        private void QueryBaseInfo()
        {
            try
            {
                CompanyType[] cs = new CompanyType[2];
                cs[0] = new CompanyType();
                cs[0].CODE = 2;
                cs[0].NAME = "男";
                cs[1] = new CompanyType();
                cs[1].NAME = "女";
                cs[1].CODE = 3;
                form.cmb_Sex.DataSource = cs;
                form.cmb_Sex.DisplayMember = "NAME";
                form.cmb_Sex.ValueMember = "CODE";
                form.cmb_Sex.DropDownStyle = ComboBoxStyle.DropDownList;

                //company 
                WebServer ws = new WebServer();
                byte[] bs = ws.GetAllDangerCompanyInfoWithCompress();
                if (bs != null)
                {
                    SysCommon.Entity.Business.DCS.View.View_DangerCompany_Ety[] ds =
                        SysCommon.Tools.CompressionHelper.DeCompressEntity(bs) as
                            SysCommon.Entity.Business.DCS.View.View_DangerCompany_Ety[];
                    if (ds != null)
                    {
                        form.cmb_CompanyName.DataSource = ds;
                        form.cmb_CompanyName.DisplayMember = "NAME";
                        form.cmb_CompanyName.ValueMember = "ID";
                        form.cmb_CompanyName.DropDownStyle = ComboBoxStyle.DropDownList;
                    }
                }
            }
            catch (Exception ex)
            {
                SysCommon.Global.mainLog.Error("调用出错", ex);
            }
        }
        private class CompanyType
        {
            private int code;
            public int CODE
            {
                get { return code; }
                set { code = value; }
            }

            private string name;
            public string NAME
            {
                get { return name; }
                set { name = value; }
            }
        }
    }
}
